import 'package:flutter/material.dart';
import 'package:magatron/develop/mine.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../const.dart';

class Course extends StatefulWidget {
  @override
  _CourseState createState() => _CourseState();
}

class _CourseState extends State<Course> with WidgetsBindingObserver {
//  InAppWebViewController webView;
  String _url = 'https://zcjpms.xiaoluxueche.com';
  var _mySubscription;
  SharedPreferences sharedPreferences;
  var signState = false;
  WebViewController _controller;
  @override
  void initState() {
    super.initState();
//     _getSp();
//     var code = Utils.getInstance().checkLogin();
//     code.then((onValue) {
//       signState = onValue;
//       setState(() {
//       });
//       if (signState == BIND) {
//         var _cityName = Utils.getInstance().getCityName(
//             sharedPreferences.get(AREA_CODE));
//         var _personNum = sharedPreferences.get(PERSON_NUM);
//         MyDio.getInstance().clearInstance();
//         MyDio.getInstance(url: BASEURL)
//             .get('${Apis.getRegionByCity}$_cityName', (data) async {
//           if (data['resultCode'] == 0) {
//             RegionCityBean regionCityBean =
//             RegionCityBean.fromJson(data['record']);
//             _url =
//             '${regionCityBean.trainingurl}api/queryTimePerson/$_personNum';
// //            webView.loadUrl(url: _url);
//             _controller.loadUrl(_url);
// //            setState(() {
// //              print('_url!!!!!:$_url');
// //              webView.loadUrl(url: _url);
// //            });
//           }
//         });
//       }
//     });
    //eventbus
//     _mySubscription = eventBus.on<EventBean>().listen((data) {
//       print('Course:${data.title}');
//       var code = Utils.getInstance().checkLogin();
//       code.then((onValue) {
//         signState = onValue;
//         setState(() {
//         });
//         if (signState == BIND) {
//           var _cityName = Utils.getInstance().getCityName(
//               sharedPreferences.get(AREA_CODE));
//           var _personNum = sharedPreferences.get(PERSON_NUM);
//           MyDio.getInstance().clearInstance();
//           MyDio.getInstance(url: BASEURL)
//               .get('${Apis.getRegionByCity}$_cityName', (data) async {
//             if (data['resultCode'] == 0) {
//               RegionCityBean regionCityBean =
//               RegionCityBean.fromJson(data['record']);
//               _url =
//               '${regionCityBean.trainingurl}api/queryTimePerson/$_personNum';
// //              webView.loadUrl(url: _url);
//               _controller.loadUrl(_url);
//             }
//           });
//         }
//       });
//     }
//     );
  }

  @override
  Widget build(BuildContext context) {
    print('_url?????:$_url');
    return Stack(
      children: <Widget>[
//        InAppWebView(
////          initialUrl: 'https://zcjpms.xiaoluxueche.com:8544/hbzcjp/api/queryTimePerson/410122196007020045',
//          initialUrl: _url,
//          initialHeaders: {},
//          initialOptions: InAppWebViewWidgetOptions(
//              inAppWebViewOptions: InAppWebViewOptions(
//                debuggingEnabled: true,
//              )
//          ),
//          onWebViewCreated: (InAppWebViewController controller) {
//            webView = controller;
//          },
//        ),

        WebView(
            initialUrl: _url,
//          initialUrl: 'https://zcjpms.xiaoluxueche.com:8543/hbzcjp/api/queryTimePerson/130527199711200867',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController controller) {
              _controller = controller;
            },
            onPageFinished: (url) {
              _controller
                  .evaluateJavascript('document.title')
                  .then((result) {});
            },
            javascriptChannels: <JavascriptChannel>[].toSet()),
        Offstage(
          offstage: signState == false ? false : true,
          child: Container(
            color: bar,
            width: MediaQuery.of(context).size.width,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '尚未绑定身份证号',
                  style: TextStyle(fontSize: 18, color: gold),
                ),
                SizedBox(
                  height: 20,
                ),
                GestureDetector(
                  onTap: () {
                    // String route = '${Routes.userBind}';
                    // Routes.router.navigateTo(context, route,
                    //     replace: false, transition: TransitionType.fadeIn);
                  },
                  child: BindButton(),
                ),
              ],
            ),
          ),
        ),
        Offstage(
          offstage: signState == false ? false : true,
          child: Container(
            width: MediaQuery.of(context).size.width,
            color: black,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '尚未登录',
                  style: TextStyle(fontSize: 18, color: gold),
                ),
                SizedBox(
                  height: 20,
                ),
                GestureDetector(
                    onTap: () {
                      // String route = '${Routes.loginMain}';
                      // Routes.router.navigateTo(context, route,
                      //     replace: false, transition: TransitionType.fadeIn);
                    },
                    child: LoginButton()),
              ],
            ),
          ),
        )
      ],
    );
  }

  _getSp() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }
}
